#!/usr/bin/evn python
# -*- coding: UTF-8 -*-
# @File: logger.py
# @Author: fy
# @date: 2021/11/7 12:33

import logging
from  logging import Logger
import os
from utils.common_fun import getProjectDir


class MyLogger:

    def __init__(self):
        self.logger = logging.getLogger(__name__)
        # 设置输出级别
        self.logger.setLevel(logging.DEBUG)

        file_dir = os.sep.join([getProjectDir(), "logs"])
        print(file_dir)
        if not os.path.exists(file_dir):
            os.mkdir(file_dir)

        #  创建文件句柄，
        fileHandle = logging.FileHandler(filename=os.sep.join([getProjectDir(), "logs", "wechat.log"]), mode='w',
                                         encoding="utf-8")
        # 格式化
        formatter = logging.Formatter("[%(asctime)s] %(levelno)s %(module)s-%(funcName)s-%(lineno)d:%(message)s")

        fileHandle.setFormatter(formatter)

        # 创建输出到console
        streamHandle = logging.StreamHandler()
        streamHandle.setFormatter(formatter)

        # 设置生效
        self.logger.addHandler(fileHandle)
        self.logger.addHandler(streamHandle)

    def get_logger(self):
        return self.logger

    # def info(self, msg):
    #     self.logger.info(msg)
    #
    # def warn(self, msg):
    #     self.logger.warn(msg)


logger = MyLogger().get_logger()


if __name__ == "__main__":
    logger.info("hello2")
